package com.feng.oauth2jwt.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author jpa配置数据源
 *
 */

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)//事务注解
public class DatabaseConfiguration {
	protected transient final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    DruidConfigEntity druidConfigEntity;
    @Bean
    public DruidDataSource dataSource() {
    	logger.debug("============================>"+JSON.toJSONString(druidConfigEntity));
    	
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(druidConfigEntity.getDriverClassName());
        dataSource.setUrl(druidConfigEntity.getUrl());
        dataSource.setUsername(druidConfigEntity.getUsername());
        dataSource.setPassword(druidConfigEntity.getPassword());
        dataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key="+druidConfigEntity.getPublickey());
        

        //配置初始化大小、最小、最大
        dataSource.setInitialSize(Integer.valueOf(druidConfigEntity.getInitialSize()));
        dataSource.setMinIdle(Integer.valueOf(druidConfigEntity.getMinIdle()));
        dataSource.setMaxActive(Integer.valueOf(druidConfigEntity.getMaxActive()));
        //配置获取连接等待超时的时间
        dataSource.setMaxWait(Long.valueOf(druidConfigEntity.getMaxWait()));

        dataSource.setValidationQuery("SELECT 'x'");
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);

        //打开PSCache，并且指定每个连接上PSCache的大小
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);

        //超过时间限制是否回收
        dataSource.setRemoveAbandoned(true);
        //超时时间；单位为秒
        dataSource.setRemoveAbandonedTimeout(30);
        //关闭abanded连接时输出错误日志
        dataSource.setLogAbandoned(true);

        //在一定毫秒数内关闭空闲连接
        dataSource.setTimeBetweenConnectErrorMillis(Long.valueOf(druidConfigEntity.getTimeBetweenEvictionRunsMillis()));
        dataSource.setMinEvictableIdleTimeMillis(Long.valueOf(druidConfigEntity.getMinEvictableIdleTimeMillis()));


//        //配置监控统计拦截的filters

        //配置防火墙拦截（支持多条更新）
        List filterList=new ArrayList<>();

        filterList.add(wallFilter());
        dataSource.setProxyFilters(filterList);

        try {
            dataSource.setFilters("stat,log4j,wall,config");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dataSource;
    }

    @Bean
    public WallFilter wallFilter(){
        WallFilter wallFilter=new WallFilter();
        wallFilter.setConfig(wallConfig());
        return wallFilter;
    }

    @Bean
    public WallConfig wallConfig(){
        WallConfig config =new WallConfig();
        config.setMultiStatementAllow(true);//允许一次执行多条语句
        //config.setNoneBaseStatementAllow(true);//允许非基本语句的其他语句
        return config;
    }

}
